import {Vue} from 'vue-property-decorator';
import $http from "@/http";
import {ElForm} from "element-ui/types/form";
import {errorMsg, successMsg, warningMsg} from "@/utils/message";

/**
 * 最早的版本不愿意改了就这样吧
 * 顶层处理类
 */
export default class BaseSysVue extends Vue {
    //大图预览
    bigImage: any = "";
    showBigImage: any = false;

    //表单数据
    public nowUpdateData: any = {};
    public nowUpdateDataIndex: any = 0;
    //表单数据
    public mFormData: any = {};
    //表格数据
    public tableData: any = [];
    //表格已选中ids
    public tableSelectIds: any = [];
    //加载框
    public tableLoading: boolean = false;
    //查询条件
    public queryParam: any = {
        pageApi: "/page",
        size: 10,
        current: 1,
        currentSize: 0,
        pages: 0,
        total: 0,
    };

    //消息对话框
    public dialogMsg: any = {
        id: "",
        ids: [],
        title: "消息通知",
        content: "是否执行当前操作？",
        isShow: false,
        open: false,
        type: "",
        param: {}//参数
    };
    //详情和修改和添加对话框
    public dialogData: any = {
        id: "",
        ids: [],
        title: "详情",
        openInfo: false,
        openEditorOrAdd: false,
        type: ""
    };

    /**
     * 执行查询分页
     */
    public basePage(pageUpdate: any): any {
        this.tableLoading = true;
        if (pageUpdate) {
            this.queryParam.size = pageUpdate.pageSize;
            this.queryParam.current = pageUpdate.currentPage;
        }
        return $http.post(this.queryParam.pageApi, this.queryParam)
            .then((res: any) => {
                if (res.success) {
                    this.tableData = res.data['records'];
                    if (this.tableData.length == 0 && this.queryParam.current > 0) {
                        this.queryParam.current = ((this.queryParam.current * 1) - (1 * 1));
                        this.basePage(null);
                        return res;
                    }
                    this.queryParam.size = res.data['size'];
                    this.queryParam.current = res.data['current'];
                    this.queryParam.currentSize = res.data['records'].length;
                    this.queryParam.pages = res.data['pages'];
                    this.queryParam.total = res.data['total'];
                    setTimeout(() => {
                        this.tableLoading = false;
                    }, 100);
                    return res;
                }
            });
    }

    /**
     * 执行修改数据
     */
    baseStartUpdate(url: any, config: any = null): any {
        (this.$refs.mFormData as ElForm).validate((valid: boolean) => {
            if (valid) {
                return $http.post(url, this.mFormData, config)
                    .then((res: any) => {
                        if (res.success) {
                            this.baseSuccessClose();
                            return res;
                        }
                    });
            } else {
                errorMsg("表单内容填写错误，请检查！")
            }
        })
    }

    /**
     * 刷新缓存信息
     */
    baseRefreshCache(url: any, data: any = {}, config: any = null): any {
        return $http.post(url, data, config)
            .then((res: any) => {
                if (res.success) {
                    setTimeout(() => {
                        successMsg("操作成功");
                        this.dialogData.openEditorOrAdd = false;
                        this.dialogMsg.isShow = false;
                        this.tableSelectIds = [];
                        this.basePage(null);
                        window.location.reload();
                    }, 400);
                    return res;
                }
            });
    }

    /**
     * 执行添加一条记录
     */
    baseStartAdd(url: any, config: any = null): any {
        (this.$refs.mFormData as ElForm).validate((valid: boolean) => {
            if (valid) {
                return $http.post(url, this.mFormData, config)
                    .then((res: any) => {
                        if (res.success) {
                            setTimeout(() => {
                                successMsg("操作成功");
                                this.dialogData.openEditorOrAdd = false;
                                this.tableSelectIds = [];
                                this.basePage(null);
                            }, 400);
                            return res;
                        }
                    });
            } else {
                errorMsg("表单内容填写错误，请检查！")
            }
        })


    }

    /**
     * 执行批量删除
     */
    public baseStartDelBatch(url: any, config: any = null): any {
        return $http.post(url, this.tableSelectIds, config)
            .then((res: any) => {
                if (res.success) {
                    setTimeout(() => {
                        successMsg("操作成功");
                        this.dialogMsg.isShow = false;
                        this.tableSelectIds = [];
                        this.basePage(null);
                    }, 400);
                    return res;
                }
            });
    }

    /**
     * 展示通知
     */
    public baseShowDialog(type: any, title: any = "通知", content: any = "", param: any = {}) {
        this.dialogMsg.type = type;
        this.dialogMsg.title = title;
        this.dialogMsg.content = content;
        this.dialogMsg.param = param;
        this.dialogMsg.isShow = true;
    }

    /**
     * 展示详情或修改 DialogData
     * @param index 数据索引
     * @param row 当前数据  type=add 时为初始化数据
     * @param type 操作类型
     * @param title 标题
     * @param initData
     */
    public baseShowDialogData(index: any = 0, row: any = {}, type: any, title: any) {
        this.nowUpdateDataIndex = index;
        this.dialogData.id = row.id;
        this.dialogData.title = title;
        this.dialogData.type = type;
        if (type == "add") {
            this.mFormData = row;
            this.dialogData.openEditorOrAdd = true;
            this.baseCleanValidate();
            return;
        }
        this.nowUpdateData = JSON.parse(JSON.stringify(row));
        this.mFormData = JSON.parse(JSON.stringify(row));
        if (type == "info") {
            this.dialogData.openInfo = true;
        }
        if (type == "editor") {
            this.dialogData.openEditorOrAdd = true;
        }
        this.baseCleanValidate();
    }

    /**
     * 展示删除 MessageDialog
     */
    public baseShowDelete(index: any, row: any, title: any, type: any, content: any = null) {
        this.dialogMsg.type = type;
        this.dialogMsg.title = title;
        this.tableSelectIds = [];
        this.tableSelectIds.push(row.id);
        this.dialogMsg.content = "确认删除 " + this.tableSelectIds.length + " 条数据？";
        if (content != null) {
            this.dialogMsg.content = content.toString();
        }
        this.dialogMsg.isShow = true;
    }

    /**
     * 展示批量删除 MessageDialog
     */
    public baseShowDeleteBatch() {
        if (this.tableSelectIds.length > 0) {
            this.dialogMsg.type = "deleteBatch";
            this.dialogMsg.title = "通知";
            this.dialogMsg.content = "确认删除 " + this.tableSelectIds.length + " 条数据？";
            this.dialogMsg.isShow = true;
        } else {
            warningMsg("至少选择一条数据.");
        }
    }

    /**
     * 还原修改数据
     */
    public baseCancelUpdate() {
        this.mFormData = JSON.parse(JSON.stringify(this.nowUpdateData));
    }

    /**
     * 复选框Change事件
     */
    public baseSelectionChange(val: Array<any>) {
        this.tableSelectIds = [];
        val.filter((it) => {
            this.tableSelectIds.push(it.id)
        });
    }

    /**
     * 清除表单验证
     */
    public baseCleanValidate() {
        if ((this.$refs.mFormData as ElForm)) {
            (this.$refs.mFormData as ElForm).clearValidate();
        }
    }

    public baseDialogMsgHandler(url: any, param: any, config: any = null) {

    }

    public baseUploadFile(url: any, param: any, config: any = null) {


    }

    public baseSuccessClose() {
        setTimeout(() => {
            successMsg("操作成功");
            this.dialogData.openEditorOrAdd = false;
            this.tableSelectIds = [];
            this.basePage(null);
        }, 400);
    }

    public baseClose() {
        setTimeout(() => {
            this.dialogData.openEditorOrAdd = false;
            this.tableSelectIds = [];
            this.basePage(null);
        }, 400);
    }

    /**
     * post 请求
     *
     * @param url 请求地址
     * @param param 请求参数 {}
     * @param config 配置 {}
     * @param refresh 请求完成后是否刷新界面
     */
    public basePost(url: any, param: any, config: any = null, refresh: boolean = true) {
        return $http.post(url, param, config)
            .then((res: any) => {
                if (res.success) {
                    setTimeout(() => {
                        if (refresh) {
                            successMsg("操作成功");
                            this.dialogMsg.isShow = false;
                            this.dialogData.openInfo = false;
                            this.dialogData.openEditorOrAdd = false;
                            this.tableSelectIds = [];
                            this.basePage(null);
                            return res;
                        } else {
                            return res;
                        }
                    }, 400);
                    return res;
                }
                return res;
            });
    }

    public msgBox(title: any = "通知", message: any = "是否执行操作？",
                  funOk: any = () => {
                  },
                  funCancel: any = () => {
                  },
                  type: any = "warning", btnOkText: any = "确定", btnCancelText: any = "取消") {
        this.$confirm(message, title, {
            confirmButtonText: btnOkText,
            cancelButtonText: btnCancelText,
            dangerouslyUseHTMLString: true,
            type: type,
        }).then(() => {
            funOk();
        }).catch(() => {
            funCancel();
        });
    }

    public msgBoxInput(title: any = "通知", message: any = "是否执行操作？",
                       funOk: any = (value: any) => {
                       },
                       funCancel: any = () => {
                       },
                       type: any = "warning", btnOkText: any = "确定", btnCancelText: any = "取消") {
        this.$prompt(message, title, {
            confirmButtonText: btnOkText,
            cancelButtonText: btnCancelText,
        }).then((val: any) => {
            funOk(val.value);
        }).catch(() => {
            funCancel();
        });
    }
}
